home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5892 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: undergrad.math.uwaterloo.ca!clgonsal
  3. From: clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves)
  4. Subject: Functions as parameters
  5. Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
  6. Message-ID: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
  7. Date: Wed, 21 Feb 1996 16:40:53 GMT
  8. Nntp-Posting-Host: cayley.uwaterloo.ca
  9. Organization: University of Waterloo
  10.  
  11.  
  12. I've often seen and even written code that uses function pointers like
  13. this:
  14.  
  15.  
  16. /* cut here */
  17. #include <stdio.h>
  18.  
  19. void foo( int (*func)( int ) ){
  20.   printf("%d\n", func( 5 ) );
  21. }
  22.  
  23. int bar( int x ){
  24.   return x*2;
  25. }
  26.  
  27. int
  28. main(){
  29.   foo( bar );
  30.   return 0;
  31. }
  32. /* cut here */
  33.  
  34. Recently though, I saw a similar piece of code that did something like
  35. this:
  36.  
  37. /* cut here */
  38. void foo( int func( int ) ){
  39.   printf("%d\n", func( 5 ) );
  40. }
  41. /* cut here */
  42.  
  43. I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
  44. worked. So my question is: is this standard, or is this some weird compiler
  45. extension. I've never seen this syntax before. Does it mean the same thing
  46. as the first piece of code? The body of the two functions is identical, and
  47. they're called in the same way.
  48.  
  49. If they are identical, why is the (*func)(int) syntax so much more common?
  50. The func(int) syntax does seem easier to type as well as read, but I've 
  51. never seen it before.
  52.  
  53. -- 
  54.         Carl Laurence Gonsalves - clgonsal@undergrad.math.uwaterloo.ca
  55.                    Computer Science, University of Waterloo
  56.                http://www.undergrad.math.uwaterloo.ca/~clgonsal/
  57.                    http://www.csclub.uwaterloo.ca/~clgonsal/
  58.